<!--
 * @Descripttion: (产品类型配置表/cks_product_type_config)
 * @Author: (admin)
 * @Date: (2025-08-13)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item>
        <el-form-item :label="$t('produce.producetypePage.page.typeName')" prop="typeDesc">
          <el-input v-model="queryParams.typeDesc" @keyup.enter="handleQuery" :placeholder="$t('produce.producetypePage.page.typeNameInput')" clearable style="width: 260px" />
        </el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['produce:cksproducttypeconfig:add']" plain icon="plus"
          @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
    </el-row>

    <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
      highlight-current-row @sort-change="sortChange">

      <el-table-column v-for="col in columns.filter(o => o.visible)" :key="col.prop" :prop="col.prop" :label="col.label"
        align="center" :width="col.width || 'auto'" :show-overflow-tooltip="true" />

      <el-table-column :label="$t('btn.operate')" width="200" align="center">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit"
            v-hasPermi="['produce:cksproducttypeconfig:edit']" @click="handleUpdate(scope.row)">{{ $t('btn.edit') }}</el-button>
          <el-button type="danger" size="small" icon="delete"
            v-hasPermi="['produce:cksproducttypeconfig:delete']" @click="handleDelete(scope.row)">{{ $t('btn.delete') }}</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改产品类型配置表对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="270px" class="mt10">

        <el-form-item :label="$t('produce.producetypePage.tableColumns.typeCodeRegex')" prop="codeRegExp" required>
          <el-input v-model="form.codeRegExp" :placeholder="$t('produce.producetypePage.formDialog.typeCodeRegex')" />
        </el-form-item>

        <el-form-item :label="$t('produce.producetypePage.tableColumns.typeID')" prop="typeID" required>
          <el-input v-model="form.typeID" :placeholder="$t('produce.producetypePage.formDialog.typeID')" />
        </el-form-item>

        <el-form-item :label="$t('produce.producetypePage.tableColumns.typeDesc')" prop="typeDesc" required>
          <el-input v-model="form.typeDesc" :placeholder="$t('produce.producetypePage.formDialog.typeDesc')" />
        </el-form-item>

        <el-form-item :label="$t('produce.producetypePage.tableColumns.remark')" prop="remark">
          <el-input v-model="form.remark" :placeholder="$t('produce.producetypePage.formDialog.remark')" />
        </el-form-item>
      </el-form>

      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="cksproducttypeconfig">
import {
  listCksProductTypeConfigConfig,
  addCksProductTypeConfigConfig, delCksProductTypeConfigConfig,
  updateCksProductTypeConfigConfig, getCksProductTypeConfigConfig,
}
  from '@/api/business/cksproducttypeconfig.js'
import dayjs from 'dayjs'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const today = dayjs()

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
  typeDesc: '',
  dateRange: [today.startOf('day').format('YYYY-MM-DD HH:mm:ss'), today.endOf('day').format('YYYY-MM-DD HH:mm:ss')],
})
const columns = ref([
  { visible: true, prop: 'id', label: 'Id' },
  { visible: true, prop: 'codeRegExp', label: proxy.$t('produce.producetypePage.tableColumns.typeCodeRegex') },
  { visible: true, prop: 'typeID', label: proxy.$t('produce.producetypePage.tableColumns.typeID') },
  { visible: true, prop: 'typeDesc', label: proxy.$t('produce.producetypePage.tableColumns.typeDesc') },
  { visible: true, prop: 'remark', label: proxy.$t('produce.producetypePage.tableColumns.remark') },
  { visible: true, prop: 'created', label: proxy.$t('produce.producetypePage.tableColumns.created') },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

function getList() {
  loading.value = true
  const formBody = getQueryBody()
  listCksProductTypeConfigConfig(formBody).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

function getQueryBody() {
  const { dateRange, pageNum, pageSize } = queryParams
  const body = { pageNum, pageSize }
  proxy.addDateRange(body, dateRange)
  return body
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    codeRegExp: [
      { required: true, message: proxy.$t('produce.producetypePage.formDialogVerify.typeCodeRegex'), trigger: "blur", type: "string" },
    ],
    typeID: [
      { required: true, message: proxy.$t('produce.producetypePage.formDialogVerify.typeID'), trigger: "blur", type: "int" },
    ],
    typeDesc: [
      { required: true, message: proxy.$t('produce.producetypePage.formDialogVerify.typeDesc'), trigger: "blur", type: "string" },
    ],
    remark: [
      { required: false, message: proxy.$t('produce.producetypePage.formDialogVerify.remark'), trigger: "blur", type: "string" },
    ],
  },
});

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    codeRegExp: null,
    typeID: 0,
    typeDesc: null,
    remark: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = proxy.$t('produce.producetypePage.formDialog.addTitle')
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getCksProductTypeConfigConfig(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = proxy.$t('produce.producetypePage.formDialog.editTitle')
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateCksProductTypeConfigConfig(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      } else {
        addCksProductTypeConfigConfig(form.value).then((res) => {
          proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
          open.value = false
          getList()
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm(proxy.$t('common.deleteConfirm', [Ids]), proxy.$t("common.systemTips"), {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delCksProductTypeConfigConfig(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess(proxy.$t('common.operateSuccess'))
    })
}

handleQuery()

</script>